package controllers.account;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lib_model.Account.Account;
import lib_model.Account.Account_warehouse;
import lib_model.Orther.ReturnObj;

/**
 * Servlet implementation class AddFactoties
 */
@WebServlet("/account/AddAccountWarehouse")
public class AddAccountWarehouse extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddAccountWarehouse() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String name = request.getParameter("txtAcc");
			String role = request.getParameter("txtRole");
			int type = (Integer.parseInt(request.getParameter("txtType")));
			RequestDispatcher dis = request
					.getRequestDispatcher("../administrator/account-warehouse.jsp");
			if (name != null && role != null
					&& request.getParameter("txtType") != null) {
				Account f = new Account();
				f.username = name;
				f = (Account) f.GetInformation().ObjResult;
				if (f == null)
					request.setAttribute("Mes", "This Account not exist !!!");
				else {
					if (f.group_id_ref != 4) {
						request.setAttribute("Mes",
								"This Account not <font color='red'>permission</font> !!!");
					} else {
						Account_warehouse ware = new Account_warehouse();
						ware.username_ref = name;
						ware.role = role;
						ware.warehouse_id_ref = type;
						ReturnObj obj = ware.Add();
						if (obj.Result == true) {
							request.setAttribute("Mes", "Suscessfull !");
						} else {
							request.setAttribute("Mes",
									"This Account is using !!!");
						}
					}
					
				}
			} else {
				request.setAttribute("Mes",
						"Error ! Can't create account-warehouse");
			}
			dis.forward(request, response);
		} catch (NumberFormatException e) {
			RequestDispatcher dis = request
					.getRequestDispatcher("../administrator/account-warehouse.jsp");
			request.setAttribute("Mes",
					"Error ! Can't create account-factories");
			dis.forward(request, response);
			e.printStackTrace();
		}
	}
}
